package ma.ensao.evalmetrics.controller.metrics;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import ma.ensao.evalmetrics.model.CLOC;
import ma.ensao.evalmetrics.model.LOC;
/* M�triques de taille de l'objet logiciel
* CLOC : mesure le nombre de lignes de commentaires
*/


public class CLOCManager{
	private CLOC noc =new CLOC();
	String ligne;
	int allLine = 0;
	int commentline = 0;
	public CLOCManager() {
		
	}
	public CLOC read(File file){
		noc.setCmt(0);
		allLine = 0;
		commentline = 0;
		InputStream ips = null;
		try {
			ips = new FileInputStream(file);
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		InputStreamReader ipsr = new InputStreamReader(ips);
		BufferedReader br = new BufferedReader(ipsr);
		try {
			
			while ((ligne = br.readLine()) != null) {
				
				if(!ligne.isEmpty() ){ 
					if(!(ligne.contains("//")) && !(ligne.contains("/*"))){
					allLine++;
					}
			
				ligne.trim();
				
				if(ligne.contains("//")){
					
					commentline ++;
					}
				if(ligne.contains("/*")){
					if(ligne.contains("*/")){
						commentline ++;
						
					}
					else{
					commentline ++;
						
					BufferedReader r = br;
					while(!r.readLine().contains("*/")){
						commentline ++;
					}
					commentline ++;
					}
				}
				}		
			}
						
			br.close();
			ipsr.close();
			ips.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		noc.setCmt(commentline);
		return noc;
	}
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		//test a = new test("C:/Users/Hosni/Documents/test/AdminManager.java");

		File f=new File("C:/AdminManager.java");
		CLOCManager a=new CLOCManager();
		System.out.println(a.read(f).getCmt());

		 
	}
}